package sg.edu.nus.iss.billing.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.util.Properties;

import javax.swing.*;
import javax.swing.border.*;

import sg.edu.nus.iss.billing.BillingApplication;
import sg.edu.nus.iss.billing.BillingConstants;
import sg.edu.nus.iss.billing.exceptions.ControllerException;

/**
 * @author Yeo Hee Ling, Ananda, Saravana
 *
 */
public class LoginWindow extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	// Declaration of Global Variables.
	private Properties properties;
	private BillingApplication billingAppl;
	private JButton btnLogin, btnExit;
	private JPasswordField txtPassword;
	private JTextField txtUsername;
	
	// Class Constructor.
	public LoginWindow(BillingApplication billingAppl) {
		super("One# Billing Application");
		this.billingAppl = billingAppl;
		
		loadConfiguration();
		
		JPanel container = new JPanel();
		container.add(headerPanel(), BorderLayout.NORTH);
		container.add(loginPanel(), BorderLayout.CENTER);
		container.add(buttonPanel(), BorderLayout.SOUTH);
		container.setBorder(new LineBorder(Color.BLACK));
		addWindowListener(windowListener);
		
		this.getContentPane().add(container);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screen.width/2) - (StyleConstants.LOGIN_WINDOW_WIDTH/2);
		int y = (screen.height/2) - (StyleConstants.LOGIN_WINDOW_HEIGHT/2);
		this.setMinimumSize(new Dimension(StyleConstants.LOGIN_WINDOW_WIDTH, StyleConstants.LOGIN_WINDOW_HEIGHT));
		this.setBounds(x, y, StyleConstants.LOGIN_WINDOW_WIDTH, StyleConstants.LOGIN_WINDOW_HEIGHT);

		this.setUndecorated(true);
		this.setResizable(false);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnLogin) {
			String strUsername = txtUsername.getText().trim();
			char[] cPassword = txtPassword.getPassword();
			performLogin(strUsername, cPassword);
		}
		else if(e.getSource() == btnExit) {
			this.billingAppl.shutdown();
		}
	}
	
	private void loadConfiguration() {
		try {
			properties = new Properties();
			FileInputStream in = new FileInputStream(BillingConstants.PROPERTIES_APPLICATION);
			properties.load(in);
			in.close();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private JPanel headerPanel() {
		ImageIcon imgHeader = new ImageIcon("./images/header.jpg");
		JLabel lblHeader = new JLabel(imgHeader);
		
		JPanel aPanel = new JPanel();
		aPanel.setLayout(new GridLayout(1, 1, 0, 0));
		aPanel.add(lblHeader);
		return aPanel;
	}
	
	private JPanel loginPanel() {
		JPanel aPanel = new JPanel();
		
		txtUsername = new JTextField(20);
		/*@DBG@*/txtUsername.setText("MsSwarna");
		aPanel.add(new JLabel(properties.getProperty("label.login.username")));
		aPanel.add(txtUsername);
		
		JPanel bPanel = new JPanel();
		txtPassword = new JPasswordField(20);
		/*@DBG@*/txtPassword.setText("MsSwarna1");
		txtPassword.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER) {
					performLogin(txtUsername.getText().trim(), txtPassword.getPassword());
				}
			}
		});
		bPanel.add(new JLabel(properties.getProperty("label.login.password")));
		bPanel.add(txtPassword);
		
		JPanel cPanel = new JPanel();
		cPanel.setLayout(new GridLayout(2, 1, 0, 0));
		cPanel.add(aPanel);
		cPanel.add(bPanel);
		return cPanel;
	}
	
	private JPanel buttonPanel() {
		JPanel aPanel = new JPanel();
		aPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		
		btnLogin = new JButton(properties.getProperty("label.login.button.login"));
		btnLogin.setPreferredSize(new Dimension(175, 25));
		btnLogin.addActionListener(this);
		aPanel.add(btnLogin);
		
		btnExit = new JButton(properties.getProperty("label.login.button.exit"));
		btnExit.setPreferredSize(new Dimension(175, 25));
		btnExit.addActionListener(this);
		aPanel.add(btnExit);
		
		return aPanel;
	}
	
	private void performLogin(String strUsername, char[] cPassword) {
		boolean validUser = false;
		
		try {
			validUser = this.billingAppl.doLogin(strUsername, new String(cPassword));
		}
		catch(ControllerException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), properties.getProperty("title.login.unsuccessful"), JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		if(validUser) {
			billingAppl.displayMainMenu();
			//Landing Page
		}
		else {
			JOptionPane.showMessageDialog(this, properties.getProperty("error.message.invalidLogin"), properties.getProperty("title.login.unsuccessful"), JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private WindowListener windowListener = new WindowAdapter () {
        public void windowClosing (WindowEvent e) {
        	billingAppl.shutdown();
        }
    };
}